package com.foreknow.elm.service.impl;

import com.foreknow.elm.dao.DeliveryaddressDao;
import com.foreknow.elm.dao.impl.DeliveryaddressDaoImpl;
import com.foreknow.elm.po.Deliveryaddress;
import com.foreknow.elm.service.DeliveryaddressService;
import com.foreknow.elm.util.DBUtil;

import java.util.ArrayList;
import java.util.List;

public class DeliveryaddressServiceImpl implements DeliveryaddressService {
    /**
     * 根据用户编号查询所属送货地址
     * @param userId
     * @return
     */
    @Override
    public List<Deliveryaddress> listDeliveryAddressByUserId(String userId) {
        List<Deliveryaddress> list = new ArrayList<>();
        DeliveryaddressDao deliveryaddressDao = new DeliveryaddressDaoImpl();
        try {
            //数据库链接
            DBUtil.getConnection();
            list =deliveryaddressDao.listDeliveryAddressByUserId(userId);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            // 关闭Connection
            DBUtil.close();
        }
        return list;
    }

    /**
     * 根据送货地址编号查询送货地址
     * @param daId
     * @return
     */
    @Override
    public Deliveryaddress getDeliveryAddressById(Integer daId) {
        DeliveryaddressDao deliveryaddressDao = new DeliveryaddressDaoImpl();
        Deliveryaddress deliveryaddress=null;
        try {
            //连接数据库
            DBUtil.getConnection();
            deliveryaddress=deliveryaddressDao.getDeliveryAddressById(daId);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            // 关闭Connection
            DBUtil.close();
        }
        return deliveryaddress;
    }

    /**
     * 向送货地址表中添加一条记录
     * @param deliveryaddress
     * @return
     */
    @Override
    public int saveDeliveryAddress(Deliveryaddress deliveryaddress) {
        DeliveryaddressDao deliveryaddressDao = new DeliveryaddressDaoImpl();
        int result =0;
        try {
            //连接数据库
            DBUtil.getConnection();
            result =deliveryaddressDao.saveDeliveryAddress(deliveryaddress);
            DBUtil.commitTransaction();
        } catch (Exception e) {
            try {
                DBUtil.rollbackTransaction();
            }catch (Exception ex){
                e.printStackTrace();
            }
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return result;
    }

    @Override
    public int updateDeliveryAddress(Deliveryaddress deliveryaddress) {
        DeliveryaddressDao deliveryaddressDao = new DeliveryaddressDaoImpl();
        int result =0;
        try {
            //连接数据库
            DBUtil.getConnection();
            result =deliveryaddressDao.updateDeliveryAddress(deliveryaddress);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return result;
    }

    @Override
    public int removeDeliveryAddress(Integer daId) {
        DeliveryaddressDao deliveryaddressDao = new DeliveryaddressDaoImpl();
        int result =0;
        try {
            //连接数据库
            DBUtil.getConnection();
            result =deliveryaddressDao.removeDeliveryAddress(daId);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            DBUtil.close();
        }
        return result;
    }
}
